package com.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.mapper.SpecialMapper;
import com.system.po.info.*;
import com.system.service.SpecialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public  class SpecialServiceImpl implements SpecialService {
    @Autowired
    private SpecialMapper specialMapper;

    @Override
    public Boolean addmessag(Information_Edit information_edit) {               //事故通知短信
        Information information=new Information();


        List<Information_type> selectypetite = specialMapper.selectypetite(information_edit.getAccident_type());//事故类型
        List<Information_place> selecplacetite = specialMapper.selecplacetite(information_edit.getAccident_place());//事故地点
        List<Information_unit> selecunittite = specialMapper.selecunittite(information_edit.getAccident_unit());//事故单位
        List<Information_scope> selecscopetite = specialMapper.selecscopetite(information_edit.getAccident_scope());//事故范围


        information.setAccident_type(selectypetite.get(0).getTite());
        information.setAccident_unit(selecunittite.get(0).getTite());
        information.setAccident_time(information_edit.getAccident_time());
        information.setAccident_place(selecplacetite.get(0).getTite());
        information.setAccident_num(information_edit.getAccident_num());
        information.setAccident_degree(information_edit.getAccident_degree());
        information.setAccident_description(information_edit.getAccident_description());
        information.setAccident_scope(selecscopetite.get(0).getTite());
        information.setAccident_causer(information_edit.getAccident_causer());
        information.setAccident_remarks(information_edit.getAccident_remarks());
        Boolean messag=specialMapper.insertmessag(information);
        return messag;
    }

    @Override
    public List<Information_place> Information_place() {
        List<Information_place> findplace = specialMapper.findplace();
        return findplace;
    }

    @Override
    public List<Information_scope> Information_scope() {
        List<Information_scope> findscope = specialMapper.findscope();
        return findscope;
    }

    @Override
    public List<Information_type> Information_type() {
        List<Information_type> findtype = specialMapper.findtype();
        return findtype;
    }

    @Override
    public List<Information_unit> Information_unit() {
        List<Information_unit> findunit = specialMapper.findunit();
        return findunit;
    }

    @Override
    public List<Personnel> querypersonnel() {                   //全部事故通知人员
        List<Personnel> personnel=specialMapper.querypersonnel();
        return personnel;
    }

    @Override
    public List<String> editsms(String content) {                //短信发送
        //编辑短信内容
        List<String> list=new ArrayList<>();
        List<Personnel> personnel=specialMapper.querypersonnel();
        for (int i=0;i<personnel.size();i++){
            String phonenum = personnel.get(i).getPhonenum();
            list.add(phonenum);
            System.out.println("号码："+phonenum +"  通知内容："+content);
            Shortmessage shortmessage=new Shortmessage();
            shortmessage.setContent(content);
            shortmessage.setPhonenum(phonenum);
            Boolean message=specialMapper.editsms(shortmessage);
        }
           return list;
    }

    @Override
    public Boolean addsfile(DocumentFile documentFile) {      //增加相关文档

        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        DocumentFile documentFile2=new DocumentFile();

        documentFile2.setTite(documentFile.getTite());
        documentFile2.setAuthor(documentFile.getAuthor());
        documentFile2.setPublisher(documentFile.getPublisher());
        documentFile2.setGmt_modified(dateFormat.format(date));
        documentFile2.setRemarks(documentFile.getRemarks());
        Boolean addsfile=specialMapper.addsfile(documentFile2);
        return addsfile;
    }

    @Override
    public Boolean deletesfile(DocumentFile documentFile) {         //删除相关文档
        Boolean deletesfile= specialMapper.deletesfile(documentFile.getId());
        return deletesfile;
    }

    @Override
    public Boolean editsfile(DocumentFile documentFile) {           //修改相关文档

        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        DocumentFile documentFile2=new DocumentFile();

        documentFile2.setId(documentFile.getId());
        documentFile2.setTite(documentFile.getTite());
        documentFile2.setAuthor(documentFile.getAuthor());
        documentFile2.setPublisher(documentFile.getPublisher());
        documentFile2.setGmt_modified(dateFormat.format(date));
        documentFile2.setRemarks(documentFile.getRemarks());
        Boolean editsfile=specialMapper.editsfile(documentFile2);
        return editsfile;
    }

    @Override
    public PageInfo<DocumentFile> findByPage(int page, int limit) {        //分页查询
        PageHelper.startPage(page,limit);
        List<DocumentFile> byPage = specialMapper.findByPage();
        PageInfo<DocumentFile> page2=new PageInfo<>(byPage);
        return page2;
    }

}
